package com.shape;

import java.util.InputMismatchException;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        ShapeCalculator calculator = new ShapeCalculator();
        Scanner scanner = new Scanner(System.in);

        System.out.println("🎯 图形面积周长计算器");
        System.out.println("====================");

        // 添加一些示例数据
        addSampleData(calculator);

        boolean running = true;

        while (running) {
            displayMenu();
            System.out.print("请选择操作 (1-6): ");

            try {
                int choice = scanner.nextInt();

                switch (choice) {
                    case 1:
                        displayAllShapes(calculator);
                        break;
                    case 2:
                        displayStatistics(calculator);
                        break;
                    case 3:
                        displayByType(calculator);
                        break;
                    case 4:
                        addNewShape(calculator, scanner);
                        break;
                    case 5:
                        addSampleData(calculator);
                        break;
                    case 6:
                        running = false;
                        System.out.println("感谢使用！再见！👋");
                        break;
                    default:
                        System.out.println("❌ 无效选择，请输入1-6之间的数字");
                }
            } catch (InputMismatchException e) {
                System.out.println("❌ 输入格式错误，请输入数字");
                scanner.nextLine(); // 清除错误输入
            } catch (Exception e) {
                System.out.println("❌ 发生错误: " + e.getMessage());
            }
        }

        scanner.close();
    }

    private static void displayMenu() {
        System.out.println("\n📋 主菜单:");
        System.out.println("1. 显示所有图形");
        System.out.println("2. 显示统计信息");
        System.out.println("3. 按类型显示");
        System.out.println("4. 添加新图形");
        System.out.println("5. 添加示例数据");
        System.out.println("6. 退出程序");
    }

    private static void displayAllShapes(ShapeCalculator calculator) {
        calculator.displayAllShapes();
    }

    private static void displayStatistics(ShapeCalculator calculator) {
        calculator.displayStatistics();
    }

    private static void displayByType(ShapeCalculator calculator) {
        calculator.displayByType();
    }

    private static void addNewShape(ShapeCalculator calculator, Scanner scanner) {
        System.out.println("\n📐 选择要添加的图形类型:");
        System.out.println("1. 圆形");
        System.out.println("2. 矩形");
        System.out.println("3. 三角形");
        System.out.println("4. 正方形");
        System.out.print("请选择 (1-4): ");

        int type = scanner.nextInt();

        try {
            switch (type) {
                case 1:
                    System.out.print("请输入圆的半径: ");
                    double radius = scanner.nextDouble();
                    calculator.addShape(new Circle(radius));
                    break;
                case 2:
                    System.out.print("请输入矩形的长度: ");
                    double length = scanner.nextDouble();
                    System.out.print("请输入矩形的宽度: ");
                    double width = scanner.nextDouble();
                    calculator.addShape(new Rectangle(length, width));
                    break;
                case 3:
                    System.out.print("请输入三角形边A: ");
                    double a = scanner.nextDouble();
                    System.out.print("请输入三角形边B: ");
                    double b = scanner.nextDouble();
                    System.out.print("请输入三角形边C: ");
                    double c = scanner.nextDouble();
                    calculator.addShape(new Triangle(a, b, c));
                    break;
                case 4:
                    System.out.print("请输入正方形的边长: ");
                    double side = scanner.nextDouble();
                    calculator.addShape(new Square(side));
                    break;
                default:
                    System.out.println("❌ 无效的图形类型！");
            }
        } catch (InputMismatchException e) {
            System.out.println("❌ 输入格式错误，请输入数字");
            scanner.nextLine();
        } catch (IllegalArgumentException e) {
            System.out.println("❌ 参数错误: " + e.getMessage());
        }
    }

    private static void addSampleData(ShapeCalculator calculator) {
        System.out.println("\n📊 添加示例数据...");

        // 添加一些示例图形
        calculator.addShape(new Circle(5.0));
        calculator.addShape(new Rectangle(4.0, 6.0));
        calculator.addShape(new Triangle(3.0, 4.0, 5.0));
        calculator.addShape(new Square(4.0));
        calculator.addShape(new Circle(3.5));
        calculator.addShape(new Rectangle(2.0, 8.0));

        System.out.println("✅ 示例数据添加完成！");
    }
}